package com.ruoyi.wb.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbQuestionTitle;
import com.ruoyi.wb.enmus.WbAuditType;
import com.ruoyi.wb.mapper.WbQuestionTitleMapper;
import com.ruoyi.wb.service.IWbQuestionTitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;


/**
 * 问卷标题Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-19
 */
@Service
public class WbQuestionTitleServiceImpl implements IWbQuestionTitleService
{
    @Autowired
    private WbQuestionTitleMapper wbQuestionTitleMapper;


    /**
     * 查询问卷标题
     *
     * @param id 问卷标题主键
     * @return 问卷标题
     */
    @Override
    public WbQuestionTitle selectWbQuestionTitleById(Long id)
    {
        return wbQuestionTitleMapper.selectWbQuestionTitleById(id);
    }

    /**
     * 查询问卷标题列表
     *
     * @param wbQuestionTitle 问卷标题
     * @return 问卷标题
     */
    @Override
    public List<WbQuestionTitle> selectWbQuestionTitleList(WbQuestionTitle wbQuestionTitle)
    {
        return wbQuestionTitleMapper.selectWbQuestionTitleList(wbQuestionTitle);
    }



    /**
     * 新增问卷标题
     * 
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWbQuestionTitle(WbQuestionTitle wbQuestionTitle)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        //查询是否已经新增过
        WbQuestionTitle item = new WbQuestionTitle();
        item.setCreateUser(user.getUserId().toString());
        List<WbQuestionTitle> wbQuestionTitles = wbQuestionTitleMapper.selectWbQuestionTitleList(item);
        long count = wbQuestionTitles.stream().filter(t -> !t.getAuditType().equals(3L)).count();
        if (count>0){
            throw new RuntimeException("您已经申请过维保无法再次申请");
        }
        wbQuestionTitle.setCreateUser(user.getUserId().toString());
        wbQuestionTitle.setCreateTime(DateUtils.getNowDate());
        wbQuestionTitle.setUserName(user.getNickName());
        wbQuestionTitle.setPhone(user.getPhonenumber());
        wbQuestionTitle.setAuditType(1L);

        return wbQuestionTitleMapper.insertWbQuestionTitle(wbQuestionTitle);
    }

    /**
     * 修改问卷标题
     * @param wbQuestionTitle 问卷标题
     * @return 结果
     */
    @Override
    public int updateWbQuestionTitle(WbQuestionTitle wbQuestionTitle)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        WbQuestionTitle wbQuestionTitle1 = wbQuestionTitleMapper.selectWbQuestionTitleById(wbQuestionTitle.getId());

        if ((!ObjectUtils.isEmpty(wbQuestionTitle.getAuditType())&&
                wbQuestionTitle.getAuditType().equals(WbAuditType.BEENTERING.getCode())&&
                !wbQuestionTitle1.getAuditType().equals(WbAuditType.BEENTERING.getCode()))
        ){
            wbQuestionTitle.setIfRead(1L);
            wbQuestionTitle.setCauseOfRejection("您的我要维保申请已经通过,请保持手机畅通。");
        }

        if((!ObjectUtils.isEmpty(wbQuestionTitle.getAuditType())&&
                wbQuestionTitle.getAuditType().equals(WbAuditType.BEENTERING.getCode())&&
                !wbQuestionTitle1.getAuditType().equals(WbAuditType.BEENTERING.getCode()))||
                (!ObjectUtils.isEmpty(wbQuestionTitle.getAuditType())&&wbQuestionTitle.getAuditType().equals(WbAuditType.NOTSETTLED.getCode()))
        ){
            wbQuestionTitle.setUpdateTime(DateUtils.getNowDate());
            wbQuestionTitle.setUpdateBy(user.getNickName());
        }

        return wbQuestionTitleMapper.updateWbQuestionTitle(wbQuestionTitle);
    }

    /**
     * 批量删除问卷标题
     * @param ids 需要删除的问卷标题主键
     * @return 结果
     */
    @Override
    public int deleteWbQuestionTitleByIds(Long[] ids)
    {
        return wbQuestionTitleMapper.deleteWbQuestionTitleByIds(ids);
    }

    /**
     * 删除问卷标题信息
     * @param id 问卷标题主键
     * @return 结果
     */
    @Override
    public int deleteWbQuestionTitleById(Long id)
    {
        return wbQuestionTitleMapper.deleteWbQuestionTitleById(id);
    }

    /**
     * 根据当前用户查询问卷
     */
    @Override
    public WbQuestionTitle byUser() {
        Long userId = SecurityUtils.getLoginUser().getUserId();

        return wbQuestionTitleMapper.byUser(userId);
    }
}
